<template>
<!-- // 海报保存到相册组件全局通用 -->
  <view v-if="show" class="fixed_box poster-popup" @click.stop="closePosterPopupFn">
    <view class="poster_publicPopup">
      <image class="poster_share_img" mode="aspectFill" :src="posterObj.url" />
      <view class="keep_btn_box" @click.stop="_saveImage" hover-class="hover"
        >保存到相册</view
      >
    </view>
  </view>
</template>

<script>
export default {
  name: "posterPopup",
  props: {
    posterObj: {
      type: Object,
      required: true,
      default: () => {},
    },
  },
  data() {
    return {
      show: true,
    };
  },
  methods: {
    closePosterPopupFn () {
      this.show = false
      this.$emit('closePosterPopupFn')
    },
    _saveImage() {
      let this_ = this;
      wx.saveImageToPhotosAlbum({
        filePath: this_.posterObj.url,
        success: function (res) {
          wx.showToast({
            title: "保存成功",
            duration: 1000,
          });
          this_.homeShareShow = false;
        },
        fail(err) {
          console.log(err, "errerrerrerr");
          if (
            err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
            err.errMsg === "saveImageToPhotosAlbum:fail auth deny" ||
            err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"
          ) {
            wx.showModal({
              title: "提示",
              content: "需要您授权保存相册",
              showCancel: false,
              success: (modalSuccess) => {
                wx.openSetting({
                  success(settingdata) {
                    console.log("settingdata", settingdata);
                    if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                      wx.showModal({
                        title: "提示",
                        content: "获取权限成功,再次点击图片即可保存",
                        showCancel: false,
                      });
                    } else {
                      wx.showModal({
                        title: "提示",
                        content: "获取权限失败，将无法保存到相册哦~",
                        showCancel: false,
                      });
                    }
                  },
                  fail(failData) {
                    console.log("failData", failData);
                  },
                  complete(finishData) {
                    console.log("finishData", finishData);
                  },
                });
              },
            });
          }
        },
      });
    },
  },
};
</script>